package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> findPage(Integer page,Integer pageSize,String name){
        Page<DishDto> pageInfo = dishService.findPage(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish", key = "#dishDto.categoryId")
    public R<String> add(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.sveawith(dishDto);
//        redisTemplate.delete("dish"+dishDto.getCategoryId());
        return R.success("添加成功");
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */

    @PutMapping
    @CacheEvict(cacheNames = "dish",key = "#dishDto.categoryId")
    public R<String>  update(@RequestBody DishDto dishDto){
         dishService.updateWithFlavor(dishDto);
         //删除redis里面修改的数据
//        redisTemplate.delete("dish"+dishDto.getCategoryId());
        return R.success("修改成功");
    }

    /**
     * 根据id查询,回显数据
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findWithById(id);
        return R.success(dishDto);
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */

    @DeleteMapping
    @CacheEvict(cacheNames = "dish",allEntries = true)
     public R<String> deleteByIds( Long[] ids){
            dishService.removeByIds(Arrays.asList(ids));
        return R.success("删除成功");
     }

    /**
     * 批量启停
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,Long[] ids){
        log.info("status--------{}",status);
           dishService.statusByIds(status,ids);
        return R.success("修改成功");
    }

    /**
     * 菜品分类
     * @param categoryId
     * @return
     */

    @GetMapping("/list")
    @Cacheable(cacheNames = "dish",key = "#categoryId",unless = "#result.data.size()==0")
    public  R<List<DishDto>> list(Long categoryId){
       /* //获取redis里面的数据
        List<DishDto> data = (List<DishDto>) redisTemplate.opsForValue().get("dish"+categoryId);
        if (data!=null){
            return R.success(data);
        }*/
        //创建条件对象
        QueryWrapper<Dish> wrapper =new QueryWrapper<>();
        wrapper.eq(categoryId !=null,"category_id",categoryId);
        wrapper.eq("status",1);
        wrapper.orderByAsc("sort").orderByDesc("update_time");
        //查询结果
        List<Dish> dishList = dishService.list(wrapper);

        //拷贝数据
        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);

            QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dish_id",dish.getId());
            List<DishFlavor> list = dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(list);

            dishDtoList.add(dishDto);
        }
       /* //把数据存入rediszhon
        redisTemplate.opsForValue().set("dish"+categoryId,dishDtoList,30, TimeUnit.SECONDS);*/


        return R.success(dishDtoList);
    }
}
